package _18_剑指OfferII;

import _18_剑指OfferII.datastrcute.TreeNode;

public class _049_剑指OfferII从根节点到叶节点的路径数字之和 {

    public int sumNumbers(TreeNode root) {
        return sumNumbers(root, 0);
    }

    private int sumNumbers(TreeNode root, int prevTotal) {
        if (root == null) return 0;
        prevTotal = prevTotal * 10 + root.val;
        if (root.left == null && root.right == null) return prevTotal;
        int left = sumNumbers(root.left, prevTotal);
        int right = sumNumbers(root.right, prevTotal);
        return left + right;
    }


   /* public int sumNumbers(TreeNode root) {
        return sumNumbers(root, root.val);
    }

    public int sumNumbers(TreeNode root, int prevTotal) {
        if (root.left == null && root.right == null) return prevTotal;
        int left = 0, right = 0;
        if (root.left != null) {
            left = sumNumbers(root.left, prevTotal * 10 + root.left.val);
        }
        if (root.right != null) {
            right = sumNumbers(root.left, prevTotal * 10 + root.right.val);
        }

        return left + right;
    }*/

}
